﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Web;
using System.Web.Mvc;
using Citi.EmailSender.Domain;
using Citi.EmailSender.WebUi.Models;

namespace Citi.EmailSender.WebUi.Controllers
{
    public class EmailController : ControllerBase<EmailModel, Email>
    {
        public ActionResult Preview(int id)
        {
            var email = new Email().SelectById<Email>(id);

            Util.DownloadTempFolder = DownloadTempFolder;
            Util.NetWorkPassword = NetWorkPassword;
            Util.NetworkDomain = NetworkDomain;
            Util.NetworkUser = NetworkUser;
            Util.Proxy = Proxy;
            Util.TempFolder = TempFolder;
            Util.WebUrl = WebUrl;

            var emailData = Util.FillEmailData(email);

            return PartialView(new EmailPreviewModel
                {
                    Attachments = emailData.Attachments,
                    Bcc = emailData.Bcc,
                    BodyMessage = emailData.BodyMessage,
                    Cc = emailData.Cc,
                    From = emailData.From,
                    SentOnBehalfOfName = emailData.SentOnBehalfOfName,
                    Subject = emailData.Subject,
                    Template = emailData.Template,
                    To = emailData.To
                });
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult EmailDashboard(EmailFilterModel model)
        {
            return PartialView(model);
        }

        public ActionResult List(EmailFilterModel model)
        {
            var emails = new Email().SelectAll<Email>();

            emails = emails.Where(x => ((x.ScheduleDate.HasValue && x.ScheduleDate.Value.Date >= model.StartDate && x.ScheduleDate.Value.Date <= model.EndDate)
                                       || (x.SentOn.HasValue && x.SentOn.Value.Date >= model.StartDate && x.SentOn.Value.Date <= model.EndDate)
                                       || (x.CreatedDate.Date >= model.StartDate && x.CreatedDate.Date <= model.EndDate))
                                       && (model.ApplicationId == 0 || x.EmailData.ApplicationId == model.ApplicationId)
                                       && (model.OnBehalfOfId == 0 || x.EmailData.OnBehalfOfId == model.OnBehalfOfId)
                                       && (
                                            model.Approval == EmailFilterModel.ApprovalStatus.All
                                            || (model.Approval == EmailFilterModel.ApprovalStatus.OnlyApproved && x.Approved.HasValue && x.Approved.Value)
                                            || (model.Approval == EmailFilterModel.ApprovalStatus.OnlyNotApproved && x.Approved.HasValue && !x.Approved.Value)
                                            || (model.Approval == EmailFilterModel.ApprovalStatus.OnlyPendingApproval && !x.Approved.HasValue)
                                       )
                                       && (string.IsNullOrWhiteSpace(model.Subject) || x.EmailData.Subject.ToLower().Contains(model.Subject.ToUpper()))
                                       && (GetEmailStatus(x) == model.Status));

            var models = new EmailModel().CreateModel(emails);
            return PartialView(models);
        }

        private EmailFilterModel.SentStatus GetEmailStatus(Email email)
        {
            if (email.ScheduleDate > DateTime.Now)
                return EmailFilterModel.SentStatus.Scheduled;

            if (email.Tentatives == 3)
                return EmailFilterModel.SentStatus.Error;

            if (email.SentOn.HasValue)
                return EmailFilterModel.SentStatus.Sent;

            return EmailFilterModel.SentStatus.Scheduled;
        }

        public ActionResult Filter()
        {
            FillViewBags();
            var model = new EmailFilterModel { StartDate = DateTime.Today, EndDate = DateTime.Today };
            return PartialView(model);
        }

        private void FillViewBags(int? applicationId = null, int? onBehalfOfId = null)
        {
            ViewBag.Applications = new Application().SelectAllActive<Application>().OrderBy(x => x.FullName)
                                                .ToSelectList(x => x.FullName, x => x.Id.ToString(), "All", applicationId.HasValue ? applicationId.Value.ToString() : null);

            ViewBag.OnBehalfOfs = new OnBehalfOf().SelectAllActive<OnBehalfOf>().OrderBy(x => x.FriendlyName)
                                                  .ToSelectList(x => x.FriendlyName, x => x.Id.ToString(), "All", onBehalfOfId.HasValue ? onBehalfOfId.Value.ToString() : null);

            ViewBag.ApprovalStatus = new EmailFilterModel.ApprovalStatus().ToSelectList(true, true);
        }
    }
}
